{% extends "staff/base.html" %}
{% import 'staff/_formhelpers.html' as forms %}

{% block title %} OAuth Clients {% endblock %}

{% block main %}
<section class="content-header">
  <h1>OAuth Clients</h1>
  <ol class="breadcrumb">
    <li><i class="fa fa-dashboard"></i>OAuth Clients</li>
  </ol>
</section>

  <section class="content">
    {% include 'alerts.html' %}
      <div class="row">
        <div class="col-md-12">
          <div class="box">
            <div class="box-header">
              <h3 class="box-title">My OAuth Clients</h3>
            </div>
            <div class="box-body table-responsive no-padding">
              <table class="table table-striped">
                <tbody>
                  <tr>
                    <th>Name</th>
                    <th>Owner</th>
                    <th>Status</th>
                    <th>Client ID</th>
                    <th>Confidentiality</th>
                    <th>Redirect URIs</th>
                    <th>Default Scopes</th>
                  </tr>

                  {% for client in my_clients %}
                  <tr>
                    <td>{{ client.name }}</td>
                    <td>{{ client.user.identifier if client.user else 'None' }}</td>
                    <td><span class="label label-{{ 'primary' if client.active else 'warning' }}">{{ 'Active' if client.active else 'Pending Approval' }}</span></td>
                    <td> <a href="{{ url_for('admin.client',client_id=client.client_id) }}">{{ client.client_id }}</a></td>
                    <td>{{ 'confidential' if client.is_confidential else 'public'}}</td>
                    <td>{{ client.redirect_uris | join(', ') }}</td>
                    <td>{{ client.default_scopes | join(', ') }}</td>
                  </tr>
                  {% endfor %}
                </tbody>
              </table>
            </div>
          </div>
        </div>
      </div>
      {% if current_user.is_admin %}
      <div class="row">
        <div class="col-md-12">
          <div class="box">
            <div class="box-header">
              <h3 class="box-title">
                All OAuth Clients
              </h3>
            </div>
            <div class="box-body table-responsive no-padding">
              <table class="table table-striped">
                <tbody>
                  <tr>
                    <th>Name</th>
                    <th>Owner</th>
                    <th>Status</th>
                    <th>Client ID</th>
                    <th>Confidentiality</th>
                    <th>Redirect URIs</th>
                    <th>Default Scopes</th>
                  </tr>

                  {% for client in clients %}
                  <tr>
                    <td>{{ client.name }}</td>
                    <td>{{ client.user.identifier if client.user else 'None' }}</td>
                    <td><span class="label label-{{ 'primary' if client.active else 'warning' }}">{{ 'Active' if client.active else 'Pending Approval' }}</span></td>
                    <td> <a href="{{ url_for('admin.client',client_id=client.client_id) }}">{{ client.client_id }}</a></td>
                    <td>{{ 'confidential' if client.is_confidential else 'public'}}</td>
                    <td>{{ client.redirect_uris | join(', ') }}</td>
                    <td>{{ client.default_scopes | join(', ') }}</td>
                  </tr>
                  {% endfor %}
                </tbody>
              </table>
            </div>
          </div>
        </div>
      </div>
      {% endif %}
      <div class="row">
        <div class="col-md-12">
            <div class="box">
              <div class="box-header">
                <h3 class="box-title">New OAuth Client</h3>
              </div>
              <div class="box-body">
              {% call forms.render_form(form, action_url="", action_text='Create OAuth Client', class_='form') %}
                {{ forms.render_field(form.name, label_visible=true, placeholder='Ok Client', type='text') }}
                {{ forms.render_field(form.description, label_visible=true, placeholder='A client for OK', type='text') }}
                {{ forms.render_field(form.client_id, label_visible=true, placeholder='ok-client', type='text') }}
                {{ forms.render_field(form.client_secret, label_visible=true, readonly='readonly', type='text') }}
                {{ forms.render_checkbox_field(form.is_confidential, label_visible=true) }}
                {{ forms.render_field(form.redirect_uris, placeholder='http://localhost:8000/authorized,https://cs61a.org/oauth/authorized', label_visible=true) }}
                {{ forms.render_field(form.default_scopes, placeholder='email,all', label_visible=true) }}
              {% endcall %}
              </div>
          </div>
        </div>
      </div>
    </section>
{% endblock %}
